package com.liukai.novel_rack.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.entities.NovelRack;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_commons.utils.JSONUtil;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_info.call_back.INovelCallBack;
import com.liukai.novel_info.presenter.impl.NovelPresenterImpl;
import com.liukai.novel_rack.R;

import java.util.List;


public class NovelRackAdapter extends RecyclerView.Adapter<NovelRackAdapter.NovelRackViewHolder> {

    private Context mContext;
    private List<NovelRack> mNovelRacks;
    private NovelPresenterImpl mNovelPresenter;


    public NovelRackAdapter(Context context) {
        mContext = context;
    }

    public NovelRackAdapter() {
    }

    public NovelRackAdapter(Context context, List<NovelRack> novelRackList) {
        this.mNovelRacks = novelRackList;
        mContext = context;

    }

    @NonNull
    @Override
    public NovelRackViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = View.inflate(mContext, R.layout.item_book_rack, null);
        return new NovelRackViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NovelRackViewHolder holder, int position) {
        final NovelRack novelRack = mNovelRacks.get(position);
        String novelId = novelRack.getNovelId();
        mNovelPresenter.findNovelById(novelId);
//        Glide.with(mContext).
//                load()
//                .into(holder.mIvNovelImg);
//        holder.mTvRecentRead.setText(novelRack.getRecentlyChapter());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnNovelListener.onNovelClickListener(novelRack);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mOnNovelListener.onNovelLongClickListener(novelRack,v);
                return true;
            }
        });


    }

    @Override
    public int getItemCount() {
        return mNovelRacks.size();
    }


    class NovelRackViewHolder extends RecyclerView.ViewHolder implements INovelCallBack {

        public TextView mTvAuthorName;
        public TextView mTvNovelName;
        public TextView mTvRecentRead;
        public ImageView mIvNovelImg;

        public NovelRackViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvNovelName = itemView.findViewById(R.id.novel_name);
            mTvRecentRead = itemView.findViewById(R.id.recent_read);
            mTvAuthorName = itemView.findViewById(R.id.author_name);
            mIvNovelImg = itemView.findViewById(R.id.novel_img);
            mNovelPresenter = new NovelPresenterImpl();
            mNovelPresenter.registerCallback(this);

        }

        /**
         * 获取返回的小说信息
         *
         * @param result
         */
        @Override
        public void findNovelByIdResult(Result result) {
            LogUtils.e(getClass(), "========>" + result.toString());
            if (result.getCode() != 200) {
                return;
            }
            NovelInfo novelInfo = JSONUtil.resolverResult(result, NovelInfo.class);
            mTvNovelName.setText(novelInfo.getNovelName());
            mTvAuthorName.setText(novelInfo.getNovelAuthor());
            Glide.with(mContext).
                    load(Constans.IMAGE_BASE_URL + novelInfo.getNovelImg())
                    .into(mIvNovelImg);
        }

        @Override
        public void findNovelChapterByIdResult(Result result) {

        }

    }

    // 更新书架信息
    public void setNovelRacks(List<NovelRack> novelRacks) {
        this.mNovelRacks.clear();
        this.mNovelRacks.addAll(novelRacks);
        notifyDataSetChanged();
    }

    // 添加书架信息
    public void addNovelRack(NovelRack novelRack) {
        this.mNovelRacks.add(novelRack);
        notifyDataSetChanged();
    }

    public interface OnNovelListener{
        void onNovelClickListener(NovelRack novelRack);

        void onNovelLongClickListener(NovelRack novelRack,View view);
    }

    // 小说点击事件接口
    private OnNovelListener mOnNovelListener;

    public void setOnNovelListener(OnNovelListener onNovelListener) {
        mOnNovelListener = onNovelListener;
    }

}
